package com.zgc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zgc.common.R;
import com.zgc.entity.RolePermission;
import com.zgc.service.RolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/rolePermission")
public class RoleAndPermissionController {
    @Autowired
    private RolePermissionService rolePermissionService;
    /**
     * 保存中间表
     *
     * @param
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Map hashmap) {
        String roleid= (String) hashmap.get("roleid");
        ArrayList list= (ArrayList) hashmap.get("perIds");
        rolePermissionService.saveAll(roleid,list);
        return R.success("权限配置成功");
    }
    //数据的回填
    @GetMapping("/{roleid}")
    public R<List> getByPermissionByRoleId(@PathVariable String roleid) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new
                LambdaQueryWrapper();
//添加排序条件
        queryWrapper.eq(RolePermission::getRoleId,roleid);
        List<RolePermission> list= rolePermissionService.list(queryWrapper);
        List<String> listnew=new ArrayList<>();
        for (RolePermission rolePermission:list) {
            Long perid=rolePermission.getPermissionId();
            listnew.add(String.valueOf(perid));
        }
        return R.success(listnew);
    }
}
